from api_server.constant.constant_api import ApiConstants
from crawler_core.constant.constants import Constants
from crawler_core.utils.request_utils import RequestUtils
from lib.sys.logger_factory import LoggerFactory


class ApiRecruit:
    logger = LoggerFactory.getLogger(" ApiRecruit", "download")

    @staticmethod
    def get_account(corp_code, source):
        param = {}
        if corp_code:
            param["corpCode"] = corp_code
        if source:
            param["source"] = source
        data = RequestUtils.get(ApiConstants.RECRUIT_GET_ACCOUNT, param)
        _list = data["data"]["data"]
        return _list

    @staticmethod
    def get_search_account(corpCodes):
        param = {
            "corpCodes": corpCodes
        }
        json_data = RequestUtils.get(ApiConstants.RECRUIT_GET_ACCOUNT, param)
        result = json_data.get("data")
        if result.get('type') == 'success':
            return result.get("data")
        return None

    @staticmethod
    def resume_down_callback(data):
        RequestUtils.post(ApiConstants.RECRUIT_RESUME_CALLBACK, data)
        ApiRecruit.logger.info("ApiRecruit -- 下载返回，反馈数据 %s" % data)

    @staticmethod
    def resume_search_callback(data_list):
        # @TODO 这是一个 data_list 需要确认
        if data_list and len(data_list) > 0:
            for data in data_list:
                RequestUtils.post(ApiConstants.RECRUIT_RESUME_CALLBACK, data)
            ApiRecruit.logger.info("ApiRecruit -- 查询返回，执行子任务结束...")

    @staticmethod
    def resume_down_list(source):
        param = {"source": source}
        json_data = RequestUtils.get(ApiConstants.RECRUIT_RESUME_DOWNLOAD, param)
        _list = json_data.get("data").get("data")
        return _list

    # 获取关键词
    @staticmethod
    def get_search_keyword():
        json_data = RequestUtils.get(ApiConstants.KEYWORD)
        result = json_data.get("data")
        if result.get('type') == 'success':
            return result.get("data")
        return None

    # 获取详细信息
    @staticmethod
    def get_search_info(keyword, corpCodes):
        param = {
            'keyword': keyword,
             'corpCodes': corpCodes
         }
        json_data = RequestUtils.get(ApiConstants.RECRUIT_RESUME_SEARCH, param)
        result = json_data.get("data")
        if result.get('type') == 'success':
            return result.get("data")
        return None
